﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using Model;//Please add references
namespace DAL
{
    /// <summary>
    /// 数据访问类:Subcribe
    /// </summary>
    public partial class SubcribeDAO
    {
        public SubcribeDAO()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("YID", "Subcribe");
        }



        public DataSet YyCount(string fromusername)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from  Subcribe where FromUserName=@FromUserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@FromUserName", SqlDbType.VarChar,50)};
            parameters[0].Value = fromusername;
            DataSet count = DbHelperSQL.Query(strSql.ToString(), parameters);
            return count;

        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int YID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Subcribe");
            strSql.Append(" where YID=@YID");
            SqlParameter[] parameters = {
					new SqlParameter("@YID", SqlDbType.Int,4)
			};
            parameters[0].Value = YID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Subcribe model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Subcribe(");
            strSql.Append("City,StoreName,YyDate,YyTime,Name,FromUserName,PersonNo,MasseurName,TjTime,Item,Phone,Result,Type,BookingId,Storetel)");
            strSql.Append(" values (");
            strSql.Append("@City,@StoreName,@YyDate,@YyTime,@Name,@FromUserName,@PersonNo,@MasseurName,@TjTime,@Item,@Phone,@Result,@Type,@BookingId,@Storetel)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@StoreName", SqlDbType.NVarChar,50),
					new SqlParameter("@YyDate", SqlDbType.VarChar,50),
					new SqlParameter("@YyTime", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
                    new SqlParameter("@Item", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@Result", SqlDbType.VarChar,50),
					new SqlParameter("@Type", SqlDbType.Int,4),                
                    new SqlParameter("@FromUserName", SqlDbType.VarChar,50),
                    new SqlParameter("@PersonNo", SqlDbType.VarChar,50),
					new SqlParameter("@MasseurName", SqlDbType.NVarChar,50),
					new SqlParameter("@TjTime", SqlDbType.DateTime)  , 
                    new SqlParameter("@BookingId", SqlDbType.Int,4),
                                        new SqlParameter("@Storetel", SqlDbType.VarChar,20)};
            parameters[0].Value = model.City;
            parameters[1].Value = model.StoreName;
            parameters[2].Value = model.YyDate;
            parameters[3].Value = model.YyTime;
            parameters[4].Value = model.Name;
            parameters[5].Value = model.Item;
            parameters[6].Value = model.Phone;
            parameters[7].Value = model.Result;
            parameters[8].Value = model.Type;
            parameters[9].Value = model.FromUserName;
            parameters[10].Value = model.PersonNo;
            parameters[11].Value = model.MasseurName;
            parameters[12].Value = model.TjTime;
            parameters[13].Value = model.BookingId;
            parameters[14].Value = model.Storetel;


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Subcribe model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Subcribe set ");
            strSql.Append("City=@City,");
            strSql.Append("StoreName=@StoreName,");
            strSql.Append("YyDate=@YyDate,");
            strSql.Append("YyTime=@YyTime,");
            strSql.Append("Name=@Name,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Result=@Result,");
            strSql.Append("Item=@Item,");
            strSql.Append("FromUserName=@FromUserName,");
            strSql.Append("PersonNo=@PersonNo,");
            strSql.Append("MasseurName=@MasseurName,");
            strSql.Append("TjTime=@TjTime,");
            strSql.Append("BookingId=@BookingId,");
            strSql.Append("Storetel=@Storetel,");
            strSql.Append("Type=@Type");

            strSql.Append(" where YID=@YID");
            SqlParameter[] parameters = {
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@StoreName", SqlDbType.NVarChar,50),
					new SqlParameter("@YyDate", SqlDbType.VarChar,50),
					new SqlParameter("@YyTime", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@Result", SqlDbType.VarChar,50),
                    new SqlParameter("@Item", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.Int,4),
					new SqlParameter("@YID", SqlDbType.Int,4),
                     new SqlParameter("@FromUserName", SqlDbType.VarChar,50),
                     new SqlParameter("@PersonNo", SqlDbType.VarChar,50),
					new SqlParameter("@MasseurName", SqlDbType.NVarChar,50),
					new SqlParameter("@TjTime", SqlDbType.DateTime),
                    new SqlParameter("@BookingId", SqlDbType.Int,4),
                                         new SqlParameter("@Storetel", SqlDbType.VarChar,20)};
            parameters[0].Value = model.City;
            parameters[1].Value = model.StoreName;
            parameters[2].Value = model.YyDate;
            parameters[3].Value = model.YyTime;
            parameters[4].Value = model.Name;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.Result;
            parameters[7].Value = model.Item;
            parameters[8].Value = model.Type;
            parameters[9].Value = model.YID;
            parameters[10].Value = model.FromUserName;
            parameters[11].Value = model.PersonNo;
            parameters[12].Value = model.MasseurName;
            parameters[13].Value = model.TjTime;
            parameters[14].Value = model.BookingId;
            parameters[15].Value = model.Storetel;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(int bookingid,int status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Subcribe set ");
            strSql.Append("Type=@Type");

            strSql.Append(" where BookingId=@BookingId");
            SqlParameter[] parameters = {
					new SqlParameter("@Type", SqlDbType.Int,4),
                    new SqlParameter("@BookingId", SqlDbType.Int,4)};
            parameters[0].Value = status;
            parameters[1].Value =bookingid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int YID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Subcribe ");
            strSql.Append(" where YID=@YID");
            SqlParameter[] parameters = {
					new SqlParameter("@YID", SqlDbType.Int,4)
			};
            parameters[0].Value = YID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string YIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Subcribe ");
            strSql.Append(" where YID in (" + YIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public Subcribe GetModel(int YID)
        //{

        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select  top 1 YID,City,StoreName,YyDate,YyTime,Name,FromUserName,PersonNo,MasseurName,TjTime,Item,Phone,Result,Type,BookingId from Subcribe ");
        //    strSql.Append(" where YID=@YID");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@YID", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = YID;

        //    Subcribe model = new Subcribe();
        //    DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
        //    if (ds.Tables[0].Rows.Count > 0)
        //    {
        //        return DataRowToModel(ds.Tables[0].Rows[0]);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Subcribe GetModel(int bookingid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 YID,City,StoreName,YyDate,YyTime,Name,FromUserName,PersonNo,MasseurName,TjTime,Item,Phone,Result,Type,BookingId,Storetel from Subcribe ");
            strSql.Append(" where BookingId=@BookingId");
            SqlParameter[] parameters = {
					new SqlParameter("@BookingId", SqlDbType.Int,4)
			};
            parameters[0].Value = bookingid;

            Subcribe model = new Subcribe();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        public Subcribe GetModel(string FromUserName,string YyDate,string YyTime)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from Subcribe ");
            strSql.Append(" where FromUserName=@FromUserName and YyDate=@YyDate and YyTime=@YyTime  ");  //在职人员
            SqlParameter[] parameters = {
					new SqlParameter("@FromUserName", SqlDbType.VarChar,50)
                    ,new SqlParameter("@YyDate", SqlDbType.VarChar,50),
					new SqlParameter("@YyTime", SqlDbType.VarChar,50)};
            parameters[0].Value = FromUserName;
            parameters[1].Value = YyDate;
            parameters[2].Value = YyTime;
            Subcribe model = new Subcribe();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Subcribe DataRowToModel(DataRow row)
        {
            Subcribe model = new Subcribe();
            if (row != null)
            {
                if (row["YID"] != null && row["YID"].ToString() != "")
                {
                    model.YID = int.Parse(row["YID"].ToString());
                }
                if (row["City"] != null)
                {
                    model.City = row["City"].ToString();
                }
                if (row["StoreName"] != null)
                {
                    model.StoreName = row["StoreName"].ToString();
                }
                if (row["YyDate"] != null)
                {
                    model.YyDate = row["YyDate"].ToString();
                }
                if (row["YyTime"] != null)
                {
                    model.YyTime = row["YyTime"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["FromUserName"] != null)
                {
                    model.FromUserName = row["FromUserName"].ToString();
                }
                if (row["PersonNo"] != null)
                {
                    model.PersonNo = row["PersonNo"].ToString();
                }
                if (row["MasseurName"] != null)
                {
                    model.MasseurName = row["MasseurName"].ToString();
                }
                if (row["TjTime"] != null)
                {
                    model.TjTime = Convert.ToDateTime(row["TjTime"]);
                }
                if (row["Item"] != null)
                {
                    model.Item = row["Item"].ToString();
                }
                if (row["Phone"] != null)
                {
                    model.Phone = row["Phone"].ToString();
                }
                if (row["Result"] != null)
                {
                    model.Result = row["Result"].ToString();
                }
                if (row["Type"] != null && row["Type"].ToString() != "")
                {
                    model.Type = int.Parse(row["Type"].ToString());
                }

                if (row["BookingId"] != null && row["BookingId"].ToString() != "")
                {
                    model.BookingId = int.Parse(row["BookingId"].ToString());
                }
                if (row["Storetel"] != null)
                {
                    model.Storetel = row["Storetel"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select YID,City,StoreName,YyDate,YyTime,Name,FromUserName,PersonNo,MasseurName,TjTime,Item,Phone,Result,Type,BookingId,Storetel  ");
            strSql.Append(" FROM Subcribe ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" YID,City,StoreName,YyDate,YyTime,Name,FromUserName,PersonNo,MasseurName,TjTime,Item,Phone,Result,Type,BookingId ,Storetel ");
            strSql.Append(" FROM Subcribe ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Subcribe ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.YID desc");
            }
            strSql.Append(")AS Row, T.*  from Subcribe T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Subcribe";
            parameters[1].Value = "YID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

